package com.ma.chapter4.t2;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "m.test")
public class Test2State {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread("t1") {
            @Override
            public void run() {
                log.debug("running");
            }
        };
        Thread t2 = new Thread("t2") {
            @Override
            public void run() {
                while (true) { //runnable

                }
            }
        };
        t2.start();

        Thread t3 = new Thread("t3") {
            @Override
            public void run() {
                log.debug("running");
            }
        };
        t3.start();

        Thread t4 = new Thread("t4") {
            @Override
            public void run() {
                synchronized (Test2State.class) {
                    try {
                        Thread.sleep(1000000); //TIMED_WAITING   有时间的等待状态
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t4.start();

        Thread t5 = new Thread("t5") {
            @Override
            public void run() {
                try {
                    t2.join();         //WAITING   无时限的等待状态
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        t5.start();

        Thread t6 = new Thread("t6") {
            @Override
            public void run() {
                synchronized (Test2State.class) {    // 拿不到锁   BLOCKED阻塞状态
                    try {
                        Thread.sleep(1000000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t6.start();

        Thread.sleep(500);

        /**
         * 15:55:00 [main] m.test - t1 state NEW
         * 15:55:00 [main] m.test - t2 state RUNNABLE
         * 15:55:00 [main] m.test - t3 state TERMINATED
         * 15:55:00 [main] m.test - t4 state TIMED_WAITING
         * 15:55:00 [main] m.test - t5 state WAITING
         * 15:55:00 [main] m.test - t6 state BLOCKED
         */
        log.debug("t1 state {}",t1.getState());
        log.debug("t2 state {}",t2.getState());
        log.debug("t3 state {}",t3.getState());
        log.debug("t4 state {}",t4.getState());
        log.debug("t5 state {}",t5.getState());
        log.debug("t6 state {}",t6.getState());


    }

}
